package net.sunofbeach.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sunofbeach.blog.mapper.SettingMapper;
import net.sunofbeach.blog.pojo.Setting;
import net.sunofbeach.blog.response.Result;
import net.sunofbeach.blog.service.SettingService;
import net.sunofbeach.blog.utils.Constants;
import net.sunofbeach.blog.utils.TextUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Bing
 * @since 2021-11-24
 */
@Service
public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> implements SettingService {

    @Override
    public Setting findOneByKey(String managerAccountInitState) {
        if (TextUtils.isEmpty(managerAccountInitState)) {
            return null;
        }
        return baseMapper.selectOne(new QueryWrapper<Setting>().eq("set_key", managerAccountInitState));
    }

    /**
     * 获取网站头部信息
     *
     * @return
     */
    @Override
    public Result getWebSizeTitle() {
        Setting title = baseMapper.selectOne(new LambdaQueryWrapper<Setting>().eq(Setting::getSetKey, Constants.Setting.WEB_SIZE_TITLE));
        if (title == null) {
            return Result.fail().message("获取网站title失败");
        }
        return Result.ok(title).message("获取网站title成功");
    }

    /**
     * 更新网站title
     * 如果没有新增
     *
     * @param title
     * @return
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    @Override
    public Result upWebSizeTitle(String title) {
        // title 是传进来的 value，key 是固定的常量
        Setting titleFromDb = baseMapper.selectOne(new LambdaQueryWrapper<Setting>().eq(Setting::getSetKey, Constants.Setting.WEB_SIZE_TITLE));
        // 如果数据库没有,新增
        if (titleFromDb == null) {
            titleFromDb = new Setting();
            titleFromDb.setSetKey(Constants.Setting.WEB_SIZE_TITLE);
            titleFromDb.setSetValue(title);
            int result = baseMapper.insert(titleFromDb);
            return result > 0 ? Result.ok().message("网站title更新成功") : Result.fail().message("网站标题更新失败");
        } else {
            // 更新
            titleFromDb.setSetKey(Constants.Setting.WEB_SIZE_TITLE);
            titleFromDb.setSetValue(title);
            int count = baseMapper.updateById(titleFromDb);
            return count > 0 ? Result.ok().message("网站标题更新成功") : Result.fail().message("网站标题更新失败");
        }
    }

    /**
     * 获取网站seo信息
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    @Override
    public Result getSeoInfo() {
        Setting description = baseMapper.selectOne(new LambdaQueryWrapper<Setting>().eq(Setting::getSetKey, Constants.Setting.WEB_SIZE_DESCRIPTION));
        Setting keyWords = baseMapper.selectOne(new LambdaQueryWrapper<Setting>().eq(Setting::getSetKey, Constants.Setting.WEB_SIZE_KEYWORDS));
        Map<String, String> result = new HashMap<>();
        result.put(description.getSetKey(), description.getSetValue());
        result.put(keyWords.getSetKey(), keyWords.getSetValue());
        return Result.ok(result).message("获取网站seo信息成功");
    }


    /**
     * 更新网站seo信息
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    @Override
    public Result putSeoInfo(String keyWords, String description) {
        // 查数据库
        Setting descriptionFromDb = baseMapper.selectOne(new LambdaQueryWrapper<Setting>().eq(Setting::getSetKey, Constants.Setting.WEB_SIZE_DESCRIPTION));
        // 数据没有信息，新增
        if (descriptionFromDb == null) {
            descriptionFromDb = new Setting();
            descriptionFromDb.setSetKey(Constants.Setting.WEB_SIZE_DESCRIPTION);
            descriptionFromDb.setSetValue(description);
            baseMapper.insert(descriptionFromDb);
        } else {
            // 否则更新
            descriptionFromDb.setSetValue(description);
            descriptionFromDb.setSetKey(Constants.Setting.WEB_SIZE_DESCRIPTION);
            baseMapper.updateById(descriptionFromDb);
        }

        Setting keyWordsFromDb = baseMapper.selectOne(new LambdaQueryWrapper<Setting>().eq(Setting::getSetKey, Constants.Setting.WEB_SIZE_KEYWORDS));
        // 数据没有信息，新增
        if (keyWordsFromDb == null) {
            keyWordsFromDb = new Setting();
            keyWordsFromDb.setSetKey(Constants.Setting.WEB_SIZE_KEYWORDS);
            keyWordsFromDb.setSetValue(keyWords);
            baseMapper.insert(keyWordsFromDb);
        } else {
            // 否则更新
            keyWordsFromDb.setSetKey(Constants.Setting.WEB_SIZE_KEYWORDS);
            keyWordsFromDb.setSetValue(keyWords);
            baseMapper.updateById(keyWordsFromDb);
        }
        return Result.ok().message("更新SEO信息成功");
    }


    /**
     * 获取网站-浏览量
     * 这个是全站的访问量，要做得细一点，还得分来源
     * 这里只统计浏览量，只统计文章的浏览量，提供一个浏览量的统计接口（页面级的）
     *
     * @return
     */
    @Override
    public Result getSizeViewCount() {
        Setting descriptionFromDb = baseMapper.selectOne(new LambdaQueryWrapper<Setting>().eq(Setting::getSetKey, Constants.Setting.WEB_SIZE_VIEW_COUNT));
        if (descriptionFromDb == null) {
            descriptionFromDb = new Setting();
            descriptionFromDb.setSetKey(Constants.Setting.WEB_SIZE_VIEW_COUNT);
            descriptionFromDb.setSetValue("1");
            baseMapper.insert(descriptionFromDb);
        }
        Map<String, Integer> result = new HashMap<>();
        result.put(descriptionFromDb.getSetKey(), Integer.valueOf(descriptionFromDb.getSetValue()));
        return Result.ok(result).message("获取网站浏览量成功");
    }


}
